package com.cssl.controller;

import com.cssl.pojo.Goods;
import com.cssl.pojo.Pages;
import com.cssl.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    private static Pages pages = new Pages();

    @GetMapping("/index_findAll")
    public List<Goods> findAll() {
        Page<Goods> p = PageHelper.startPage(1, 8);
        goodsService.index_findAll();
        return p.getResult();
    }

    @GetMapping("/index_findByType")
    public List<Goods> findByType(@RequestParam("t_id") Integer t_id) {
        System.out.println("findByType:" + t_id);
        Page<Goods> p = PageHelper.startPage(1, 8);
        goodsService.index_findByType(t_id);
        return p.getResult();
    }

    @GetMapping("/shop_findByType")
    public List<Goods> findByType2(@RequestParam("t_id") Integer t_id, @RequestParam String order) {
        Page<Goods> p = PageHelper.startPage(1, 16);
        goodsService.shop_findByType(t_id, order);
        pages.setPageNum(p.getPages());
        return goodsService.shop_findByType(t_id, order);
    }

    @GetMapping("/shop-detail_findByType")
    public List<Goods> findByType1(@RequestParam("t_id") Integer t_id) {
        System.out.println("findByType:" + t_id);
        Page<Goods> p = PageHelper.startPage(1, 4);
        goodsService.index_findByType(t_id);
        return p.getResult();
    }

    @GetMapping("/index_findById")
    public Goods findById(Integer gid) {
        return goodsService.index_findById(gid);
    }

    @PostMapping("/shop_mainSerch")
    public List<Goods> findById(@RequestParam Map<String, Object> map) {
        Page<Goods> p = PageHelper.startPage(1, 16);
        goodsService.shop_mainserch(map);
        pages.setPageNum(p.getPages());
        return goodsService.shop_mainserch(map);
    }

    @GetMapping("/shop_changeOrder")
    public List<Goods> changeOrder(@RequestParam String order) {
        Page<Goods> p = PageHelper.startPage(1, 16);
        goodsService.shop_changeOrder(order);
        pages.setPageNum(p.getPages());
        return goodsService.shop_changeOrder(order);
    }

    @GetMapping("/daily_push")
    public List<Goods> daily_push() {
        return goodsService.daily_push();
    }

    @GetMapping("/shop_findAll")
    public List<Goods> shop_findAll(@RequestParam String order) {
        Page<Goods> p = PageHelper.startPage(1, 16);
        goodsService.shop_changeOrder(order);
        System.out.println(p.getPages());
        pages.setPageNum(p.getPages());
        return goodsService.shop_changeOrder(order);
    }

    @GetMapping("/shop_getPages")
    public Integer shop_getPages() {
        System.out.println(pages.getPageNum());
        return pages.getPageNum();
    }
}
